import { reqUserAddressList, reqTradeInfoData } from '@/api'

const state = {
  userAddressList: [],
  tradeInfo: [],
}
const getters = {
  // 商品列表
  detailArrayList(state) {
    return state.tradeInfo.detailArrayList
  },
  // 商品总金额
  originalTotalAmount() {
    return state.tradeInfo.originalTotalAmount
  },
  // 折扣后的金额或是优惠后的金额
  totalAmount() {
    return state.tradeInfo.totalAmount
  },
  // 商品数量
  totalNum() {
    return state.tradeInfo.totalNum
  },
  // 订单编号
  tradeNo() {
    return state.tradeInfo.tradeNo
  },
  // 6. 优惠金额或是折扣金额 或是返现金额
  activityReduceAmount(state) {
    return state.tradeInfo.activityReduceAmount
  },
}

const actions = {
  // 获取用户的收货地址
  async getUserAddressList({ commit }) {
    const res = await reqUserAddressList()
    if (res.code === 200) {
      commit('SAVE_USER_ADDRESS', res.data)
    } else {
      console.log(res.message)
    }
  },
  // 获取用户订单信息
  async getTradeInfoData({ commit }) {
    const res = await reqTradeInfoData()
    if (res.code === 200) {
      commit('SAVE_TRADE_INFO', res.data)
    } else {
      console.log(res.message)
    }
  },
}

const mutations = {
  // 存储用户收货地址
  SAVE_USER_ADDRESS(state, payload) {
    state.userAddressList = payload
  },
  // 存储订单信息
  SAVE_TRADE_INFO(state, payload) {
    state.tradeInfo = payload
  },
}

export default {
  namespaced: true,
  state,
  actions,
  mutations,
  getters,
}
